package com.jie.server.mvc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.common.http.constant.HttpEntityClassNameConstant;
import com.jie.common.http.constant.HttpHeaderLineNameConstant;
import com.jie.common.http.constant.response.ServerResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import com.jie.common.util.JsonUtil;
import com.jie.server.core.annotation.Mapping;
import com.jie.server.mvc.model.entity.GroupChat;
import com.jie.server.mvc.model.vo.GroupChatQueryVo;
import com.jie.server.mvc.service.GroupChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 群聊表 前端控制器
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Controller("group-chat")
public class GroupChatController {

    private final GroupChatService groupChatService;

    @Autowired
    public GroupChatController(
            GroupChatService groupChatService
    ) {
        this.groupChatService = groupChatService;
    }

    /**
     * 发送群聊信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回响应信息
     */
    @Mapping
    public HttpResponse chat(HttpRequest httpRequest, HttpResponse httpResponse) {
        GroupChat groupChat = JsonUtil.fromJson(httpRequest.getEntityString(), GroupChat.class);
        // 设置发送者的id
        groupChat.setSendUserId(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.groupChatService.chat(groupChat)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_GROUP_SUCCESS);
            httpResponse.setEntity("发送成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_GROUP_FAIL);
            httpResponse.setEntity("发送失败!");
        }
        return httpResponse;
    }

    /**
     * 获取群聊记录
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回响应信息
     */
    @Mapping
    public HttpResponse records(HttpRequest httpRequest, HttpResponse httpResponse) {
        GroupChatQueryVo groupChatQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), GroupChatQueryVo.class);
        Page<GroupChat> groupChatPage = this.groupChatService.records(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)), groupChatQueryVo);
        if (groupChatPage == null) {
            httpResponse.setEntity("查询失败!");
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_GROUP_RECORDS_FAIL);
        } else {
            httpResponse.addOneHeaderLine("total", String.valueOf(groupChatPage.getTotal()));
            httpResponse.setEntity(JsonUtil.toJson(groupChatPage.getRecords()));
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.CHAT_TO_GROUP_RECORDS_SUCCESS);
        }
        return httpResponse;
    }

}
